﻿using System.Text.Json;
using System.Text.Json.Nodes;
using Dark_Core_Server.Models.Universal;
using Dark_Core_Server.Models.User;
using Dark_Core_Server.Service;
using DarkSKL_Core.ArgModules;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TouchSocket.Core;

namespace Dark_Core_Server.Controllers
{
    [Route("CoreService/[controller]:[Action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly UserService UserService;

        public UserController(UserService userService)
        {
            this.UserService = userService;
        }

        [HttpPost]
        public async Task<ActionResult> Register(SimpleUserRecord userRecord)
        {
            var result = await UserService.CreateUserAsync(userRecord);
            return Ok(result);
        }


        [HttpPost]
        public async Task<ActionResult> GrantAuthorization(SimpleUserRecord userRecord)
        {
            var result = await UserService.GrantAuthorization(userRecord);
            return Ok(result);
        }

        [HttpGet]
        [Authorize]
        public async Task<ActionResult> QuerySelfProfile()
        {
            string? token = HttpContext.Request.Headers["Authorization"];
            ResponseMsg<UserDocument> userDocument = await UserService.ParseTokenToUser(token);
            if (userDocument.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg(userDocument.Msg, userDocument.Status));
            return Ok((await UserService.QueryUserProfileAsync(UserQueryMode.SingleId, userDocument.Data.id.ToString())));
        }

        [HttpPost]
        [Authorize]
        public async Task<ActionResult> ModifySelfProfile(SimpleUserProfileRecord userProfileRecord)
        {
            string? token = HttpContext.Request.Headers["Authorization"];
            ResponseMsg<UserDocument> userDocument = await UserService.ParseTokenToUser(token);
            if (userDocument.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg(userDocument.Msg, userDocument.Status));
            return Ok((await UserService.UpdateUserProfileAsync(userDocument.Data.id, userProfileRecord)));
        }


        [HttpPost]
        [Authorize]
        public async Task<ActionResult> ModifySelfPassword(string oldPassword,string newPassword)
        {
            string? token = HttpContext.Request.Headers["Authorization"];
            ResponseMsg<UserDocument> userDocument = await UserService.ParseTokenToUser(token);
            if (userDocument.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg(userDocument.Msg, userDocument.Status));
            if(userDocument.Data.Password != oldPassword) return Ok(false.CreateResponseMsg("旧密码错误",UniversalState.Error));

            return Ok((await UserService.UpdateUserPasswordAsync(new SimpleUserRecord(userDocument.Data.UserName,newPassword))));
        }


        [HttpGet]
        [Authorize]
        public async Task<ActionResult> DeleteSelfUser()
        {
            ResponseMsg<UserDocument> userResponseMsg = await UserService.ParseHttpRequestToUser(HttpContext.Request);
            if (userResponseMsg.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg("User not found", UniversalState.Error));
            return Ok((await UserService.DeleteUserAsync(userResponseMsg.Data.ToSimpleUserRecord())));
        }


        [HttpPost]
        public async Task<ActionResult> QueryUserProfile(PostRequestArgs args)
        {
            args.args.TryGetValue("payload", out var payload);
            if(payload == null) return Ok("No payload found in request.".CreateResponseMsg("缺少负载payload", UniversalState.Error));
            args.args.TryGetValue("pageNum", out var pageNum);
            args.args.TryGetValue("pageSize", out var pageSize);
            return Ok((await UserService.QueryUserProfileAsync(args.mode.ToUserQueryMode(), payload.ToString(), Convert.ToInt32(pageNum),Convert.ToInt32(pageSize)==0?999:Convert.ToInt32(pageSize))));
        }




    }
}
